package com.jiuyin.dianjing.ui.activity.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.LogUtil;
import com.jiuyin.dianjing.util.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PASSWORD;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PASSWORD_PREV;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_POST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.util.DesUtil.getMd5;

/**
 * 修改密码
 */
public class ModifyPwdActivity extends BaseActivity {
    @BindView(R.id.edt_pwd_old)
    EditText mOldPwd;
    @BindView(R.id.edt_pwd_new)
    EditText mNewPwd;
    @BindView(R.id.edt_pwd_new_confirm)
    EditText mNewConfirmPwd;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        setTitle(R.string.setting_modify_pwd);
    }

    @OnClick({R.id.tv_pwd_forget, R.id.tv_done})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_pwd_forget:
                Bundle bundle = new Bundle();
                bundle.putSerializable(KEY_POST, ConfirmNewPwdActivity.class);
                goTargetActivity(ModifyPhoneVerifyActivity.class, bundle);
                break;
            case R.id.tv_done:
                checkMsgAndSubmit();
                break;
        }
    }

    /**
     * 检查信息,提交修改密码
     */
    private void checkMsgAndSubmit() {
        String oldPwd = mOldPwd.getText().toString();
        String newPwd = mNewPwd.getText().toString();
        String newPwdConfirm = mNewConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(oldPwd)) {
            ToastUtil.showShort(R.string.setting_phone_pwd_null_tips);
            return;
        }
        if (TextUtils.isEmpty(newPwd)) {
            ToastUtil.showShort(R.string.setting_phone_pwd_null_tips);
            return;
        }
        if (TextUtils.isEmpty(newPwdConfirm)) {
            ToastUtil.showShort(R.string.setting_phone_pwd_null_tips);
            return;
        }
        if (!newPwdConfirm.equals(newPwd)) {
            ToastUtil.showShort(R.string.setting_phone_pwd_new_pwd_tip);
            return;
        }
        setNewPwd();
    }

    /**
     * 设置新密码
     */
    private void setNewPwd() {
        String oldPwd = mOldPwd.getText().toString();
        String newPwd = mNewPwd.getText().toString();
        HashMap<String, Object> data = new HashMap<>();
        String md5PrePwd = getMd5(oldPwd);
        String md5NewPwd = getMd5(newPwd);
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_PASSWORD_PREV, md5PrePwd);
        data.put(KEY_PASSWORD, md5NewPwd);
        JSONObject jsonObject = new JSONObject(data);
        ProgressDialogCallback progress = new ProgressDialogCallback() {
            @Override
            public void showProgressDialog() {
                showLoading();
            }

            @Override
            public void dismissProgressDialog() {
                dismissLoading();
            }
        };
        ApiResultCallback request = new ApiResultCallback() {
            @Override
            public void onSuccess(JsonObject data) {
                quitAndLogin();
            }

            @Override
            public void onFail(String msg) {
                LogUtil.log("ModifyPwdActivity onFail msg = " + msg);
                ToastUtil.showShort(msg);
            }

            @Override
            public void onError(String msg) {
                LogUtil.log("ModifyPwdActivity onError msg = " + msg);
                ToastUtil.showShort(msg);
            }
        };
        ServerApi.post(ApiEnum.APP_UPDATE_PASSWORD_API.getUrl(), jsonObject, null,
                progress, d -> addDisposable(d), request);
    }
}
